﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using MVCWeb.Models;

namespace MVCWeb.Models
{
    public class OrderModel
    {
        [Required]
        [DisplayName("Order ID")]
        public int order_id { get; set; } 

        [Required]
        [DisplayName("Customer ID")]
        public int customer_id { get; set; }

        [Required]
        [DisplayName("Total Price")]
        public double total_price { get; set; }

        [Required]
        [DisplayName("Date Created")]
        public DateTime date_created { get; set; }

        [Required]
        [DisplayName("Order Placed")]
        public int is_ordered { get; set; } 

    }

    public static class OrderClientServices
    {
        //Create New Order
        public static void CreateOrder(OrderModel order)
        {
            SLOrder.Order newOrder = DTO_to_SL(order);
            SLOrder.ISLOrder SLOrder = new SLOrder.SLOrderClient();

            string[] errors = new string[0];
            SLOrder.InsertOrders(newOrder, ref errors);
        }
        
        //PlaceOrder
        public static void PlaceOrder(int id)
        {

            SLOrder.ISLOrder SLOrder = new SLOrder.SLOrderClient();

            string[] errors = new string[0];
            SLOrder.PlaceOrder(id, ref errors);

        }

        //GetTotalPrice
        public static double GetTotalPrice(int id)
        {
            SLOrder.ISLOrder SLOrders = new SLOrder.SLOrderClient();

            string[] errors = new string[0];
            double price = SLOrders.GetTotalPrice(id, ref errors);

            // this is the data transfer object code...
            return price;
        }


        //checks if order has been placed
        public static int SeeIfOrderPlaced(int order_num)
        {
            SLOrder.ISLOrder SLOrders = new SLOrder.SLOrderClient();

            string[] errors = new string[0];
            int placed = SLOrders.SeeIfOrderPlaced(order_num, ref errors);

            return placed;
        }

        public static void DeleteAllRecords()
        {
            SLOrder.ISLOrder SLOrders = new SLOrder.SLOrderClient();

            string[] errors = new string[0];
            SLOrders.DeleteAllRecords(ref errors);
        }

        public static List<OrderModel> GetCustomerOrders(int id)
        {
            List<OrderModel> orderlist = new List<OrderModel>();
            SLOrder.ISLOrder SLOrders = new SLOrder.SLOrderClient();

            string[] errors = new string[0];

            SLOrder.Order[] orders = SLOrders.GetCustomerOrders(id, ref errors);

            if (orders!= null)
            {
                foreach (SLOrder.Order o in orders)
                {
                    OrderModel item = DTO_to_PL(o);
                    orderlist.Add(item);
                }
            }

            return orderlist;
        }


        //DTO to PL
        private static OrderModel DTO_to_PL(SLOrder.Order order)
        {
            OrderModel PLOrder = new Models.OrderModel();
            PLOrder.order_id = order.order_id;
            PLOrder.customer_id = order.customer_id;
            PLOrder.total_price = order.total_price;
            PLOrder.date_created = order.date_created;
            PLOrder.is_ordered = order.is_ordered;

            return PLOrder;
        }

        //DTO to SL
        private static SLOrder.Order DTO_to_SL(OrderModel order)
        {
            SLOrder.Order SLOrder = new MVCWeb.SLOrder.Order();
            SLOrder.order_id = order.order_id;
            SLOrder.customer_id = order.customer_id;
            SLOrder.total_price = order.total_price;
            SLOrder.date_created = order.date_created;
            SLOrder.is_ordered = order.is_ordered;

            return SLOrder;
        }

    }
}
